package com.cmf.myproject;

import java.io.IOException;
import java.util.concurrent.Executor;

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author jianglu
 * @see ComponentScan使用 https://blog.csdn.net/onedaycbfly/article/details/79082730
 */
@SpringBootApplication(scanBasePackages= {"com.cmf.myproject","com.system"})
@MapperScan(basePackages = {"com.cmf.myproject.dal.mybatis.mapper","com.cmf.myproject.dal.mybatis.mymapper","com.system.boss"})
@EnableAsync
@Slf4j
public class Application {
	
	@Bean
	public Object testBean(DataSource dataSource, PlatformTransactionManager platformTransactionManager) {
		log.debug("当前dataSource: " + dataSource.getClass().getName());
		log.debug("当前TransactionManager: " + platformTransactionManager.getClass().getName());
		return new Object();
	}
	
	@Bean(name = "threadPoolTaskExecutor")
	public Executor threadPoolTaskExecutor() {
		ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
		//核心线程数
		threadPoolTaskExecutor.setCorePoolSize(10);
		//最大线程数
		threadPoolTaskExecutor.setMaxPoolSize(50);
		//队列最大长度
		threadPoolTaskExecutor.setQueueCapacity(20);
		//线程池维护线程所允许的空闲时间
		threadPoolTaskExecutor.setKeepAliveSeconds(3000);
		//线程池对拒绝任务(无线程可用)的处理策略
		threadPoolTaskExecutor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
	    return threadPoolTaskExecutor;
	}
	
	public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        try {
			Runtime.getRuntime().exec("cmd /c start http://127.0.0.1:9082/boss/login");
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}